import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import {loadOpenId, loadVillageId, loadUserId} from '../common/js/cache.js'
import qs from 'qs'
const url = `${commonUrl.apihost}index.php/home/`
//  头像资料数据
export function getMyInfo () {
  return axios({
    method: 'GET',
    url: `${url}login/getUser?openid=${loadOpenId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 获取验证码
export function getPhoneCode (phoneNumber) {
  return axios({
    method: 'GET',
    url: `${url}login/code?phone=${phoneNumber}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 保存编辑资料
export function saveEditData (name, realname, phone, sex, identity, code, email) {
  const data = {
    name,
    real_name: realname,
    phone,
    sex,
    wx_role: identity,
    village_id: loadVillageId(),
    code,
    openid: loadOpenId(),
    email
  }
  return axios({
    method: 'POST',
    url: `${url}login/perfection`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 我的房列表
export function getDatas () {
  return axios({
    method: 'GET',
    url: `${url}house_Auth/getMyHouse?openid=${loadOpenId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 房屋认证  小区  苑  幢  单元  房号
export function getHouseData (id) {
  return axios({
    method: 'GET',
    url: `${url}house_Auth/getHouse?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 保存房屋资料
export function saveHouseData (houseid, areaid) {
  const data = {
    openid: loadOpenId(),
    house_id: houseid,
    areas_id: areaid
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/setHouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 我的查验列表
export function getCheckList () {
  return axios({
    method: 'GET',
    url: `${url}examine/myExamineIndex/?user_id=${loadUserId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 删除某条查验内容
export function delCheck (id) {
  return axios({
    method: 'GET',
    url: `${url}examine/deleteExamine/?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 编辑查验内容
export function editCheckContent (id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/firstAssortInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 编辑查验内容
export function editCheckEvery (id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/secondLowCateInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 提交编辑查验内容
export function submitEditCheckEvery (id, summary) {
  const data = {
    related_id: id,
    summary: summary
  }
  return axios({
    method: 'POST',
    url: `${url}examine/editSummary`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}